
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

//index.js
//获取应用实例
const app = getApp()

// pages/ucenter/payresult/payresult.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bb:'',
    modalName:'',
    status: 0,
    orderSn: '',
    name: '@高攀，我是叶雪松，我在群里下了订单，请尽快处理',
    openShare: false,
    imageUrl: '/static/images/oder4.jpg'
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        console.log(options)
    if (options.status==0){
        this.setData({
          status:false,
          orderSn: options.orderSn,
          bb: options.bb
        })
    }
    if (options.status == 1) {
      this.setData({
        status: true,
        orderSn: options.orderSn,
        bb: options.bb
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  payOrder(){
    util.request(api.ToBePaidPay + 'orderSn=' + this.data.bb + '&id=' + this.data.orderSn, {

    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if (res.code === 200) {
          const orderSn = res.data.id
          const bb = res.data.orderSn
          const payParam = res.data.payment;
          console.log("支付过程开始");
          wx.requestPayment({
            'timeStamp': payParam.timeStamp,
            'nonceStr': payParam.nonceStr,
            'package': payParam.packageValue,
            'signType': payParam.signType,
            'paySign': payParam.paySign,
            'success': function (res) {
              console.log("支付过程成功");
              wx.redirectTo({
                url: '/pages/ucenter/payresult/payresult?status=1&orderSn=' + orderSn + '&bb=' + bb
              });
            },
            'fail': function (res) {
              console.log("支付过程失败");
              wx.redirectTo({
                url: '/pages/ucenter/payresult/payresult?status=0&orderSn=' + orderSn + '&bb=' + bb
              });
            },
            'complete': function (res) {
              console.log("支付过程结束")
            }
          });
        } else {
          // wx.redirectTo({
          //   url: '/pages/ucenter/payresult/payresult?status=0&orderSn=' + orderSn
          // });
        }
      }
      )
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  closeShare: function () {
    this.setData({
      openShare: false,
    });
  },

  onShareAppMessage: function () {
    const user = wx.getStorageSync('userInfo')
    var shareObj =  {
      title: '@老板，我是'+user.nickName+'，我在群里下了订单，请尽快处理',
      desc: '欢迎购物！',
      imageUrl: this.data.imageUrl,
      path: '/pages/index/index',
      success: function () {
        this.setData({
          openShare: false,
        });
      }
    }
    return shareObj
  },
  shareFriendOrCircle: function () {
    //var that = this;
    if (this.data.openShare === false) {
      this.setData({
        openShare: !this.data.openShare
      });
    } else {
      return false;
    }
  },
})